Sandbox vectors

Let’s define some vectors which can be used for demonstrations:

manyNumbers <- sample( 1:1000, 20 )
manyNumbers
 [1] 992 394 778 968 601 178 492 223 753 507  26 388 409 249 113 379  51 319 799 160
manyNumbersWithNA <- sample( c( NA, NA, NA, manyNumbers ) )
manyNumbersWithNA
 [1] 319 601  NA  NA 409 113 507  NA 992 753 249 799  26 492 160 379 223 778  51 968 394 178 388
duplicatedNumbers <- sample( 1:5, 10, replace = TRUE )
duplicatedNumbers
 [1] 5 3 1 5 1 2 5 1 2 1
letters
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w"
[24] "x" "y" "z"
LETTERS
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W"
[24] "X" "Y" "Z"
mixedLetters <- c( sample( letters, 5 ), sample( LETTERS, 5 ) )
mixedLetters
 [1] "c" "l" "z" "d" "q" "H" "G" "Z" "P" "Y"

Are all/any elements TRUE

  • Input: logical vector
  • Output: single logical value
  • Task: try, understand what happens when you use manyNumbersWithNA instead of manyNumbers.
all( manyNumbers <= 1000 )
[1] TRUE
all( manyNumbers <= 500 )
[1] FALSE
any( manyNumbers > 1000 )
[1] FALSE
any( manyNumbers > 500 )
[1] TRUE
all( !is.na( manyNumbers ) )
[1] TRUE
any( is.na( manyNumbers ) )
[1] FALSE

Which elements are TRUE

Input: logical vector Output: vector of numbers (positions)

which( manyNumbers > 900 )
[1] 1 4
which( manyNumbersWithNA > 900 )
[1]  9 20
which( is.na( manyNumbersWithNA ) )
[1] 3 4 8

Filtering vector elements

  • Input: any vector and filtering condition
  • Output: elements of the input vector
  • Note: several ways to get the same effect
manyNumbers[ manyNumbers > 900 ] # indexing by logical vector
[1] 992 968
manyNumbers[ which( manyNumbers > 900 ) ] # indexing by positions
[1] 992 968
somePositions <- which( manyNumbers > 900 )
manyNumbers[ somePositions ]
[1] 992 968

Are some elements among other elements

  • Input: two vectors
  • Output: a logical vector corresponding to the first input vector
"A" %in% LETTERS
[1] TRUE
c( "X", "Y", "Z" ) %in% LETTERS
[1] TRUE TRUE TRUE
all( c( "X", "Y", "Z" ) %in% LETTERS )
[1] TRUE
all( mixedLetters %in% LETTERS )
[1] FALSE
any( mixedLetters %in% LETTERS )
[1] TRUE
mixedLetters[ mixedLetters %in% LETTERS ]
[1] "H" "G" "Z" "P" "Y"
mixedLetters[ !( mixedLetters %in% LETTERS ) ]
[1] "c" "l" "z" "d" "q"
manyNumbers %in% 300:600
 [1] FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE
[16]  TRUE FALSE  TRUE FALSE FALSE
which( manyNumbers %in% 300:600 )
[1]  2  7 10 12 13 16 18
sum( manyNumbers %in% 300:600 )
[1] 7

Pick one of two (three) depending on condition

  • Input: a logical vector and two vectors additional vectors (for TRUE, for FALSE)
  • Output: elements of the additional vectors
  • Note: it can take care of NAs
if_else( manyNumbersWithNA >= 500, "large", "small" )
 [1] "small" "large" NA      NA      "small" "small" "large" NA      "large" "large" "small"
[12] "large" "small" "small" "small" "small" "small" "large" "small" "large" "small" "small"
[23] "small"
if_else( manyNumbersWithNA >= 500, "large", "small", "UNKNOWN" )
 [1] "small"   "large"   "UNKNOWN" "UNKNOWN" "small"   "small"   "large"   "UNKNOWN" "large"  
[10] "large"   "small"   "large"   "small"   "small"   "small"   "small"   "small"   "large"  
[19] "small"   "large"   "small"   "small"   "small"  
# here integer 0L is needed instead of real 0.0 
# manyNumbersWithNA contains integer numbers and the method complains
if_else( manyNumbersWithNA >= 500, manyNumbersWithNA, 0L ) 
 [1]   0 601  NA  NA   0   0 507  NA 992 753   0 799   0   0   0   0   0 778   0 968   0   0   0

Duplicates and unique elements

  • Input: a vector
unique( duplicatedNumbers )
[1] 5 3 1 2
unique( c( NA, duplicatedNumbers, NA ) )
[1] NA  5  3  1  2
duplicated( duplicatedNumbers )
 [1] FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE

Positions of max/min elements

which.max( manyNumbersWithNA )
[1] 9
manyNumbersWithNA[ which.max( manyNumbersWithNA ) ]
[1] 992
which.min( manyNumbersWithNA )
[1] 13
manyNumbersWithNA[ which.min( manyNumbersWithNA ) ]
[1] 26
range( manyNumbersWithNA, na.rm = TRUE )
[1]  26 992

Sorting/ordering of vectors

manyNumbersWithNA
 [1] 319 601  NA  NA 409 113 507  NA 992 753 249 799  26 492 160 379 223 778  51 968 394 178 388
sort( manyNumbersWithNA )
 [1]  26  51 113 160 178 223 249 319 379 388 394 409 492 507 601 753 778 799 968 992
sort( manyNumbersWithNA, na.last = TRUE )
 [1]  26  51 113 160 178 223 249 319 379 388 394 409 492 507 601 753 778 799 968 992  NA  NA  NA
sort( manyNumbersWithNA, na.last = TRUE, decreasing = TRUE )
 [1] 992 968 799 778 753 601 507 492 409 394 388 379 319 249 223 178 160 113  51  26  NA  NA  NA
manyNumbersWithNA[1:5]
[1] 319 601  NA  NA 409
order( manyNumbersWithNA[1:5] )
[1] 1 5 2 3 4
rank( manyNumbersWithNA[1:5] )
[1] 1 3 4 5 2
sort( mixedLetters )
 [1] "c" "d" "G" "H" "l" "P" "q" "Y" "z" "Z"

Ranking of vectors

manyDuplicates <- sample( 10:15, 10, replace = TRUE )
rank( manyDuplicates )
 [1]  8.5  8.5 10.0  1.0  2.5  6.0  6.0  6.0  4.0  2.5
rank( manyDuplicates, ties.method = "min" )
 [1]  8  8 10  1  2  5  5  5  4  2
rank( manyDuplicates, ties.method = "random" )
 [1]  8  9 10  1  2  5  7  6  4  3

Rounding numbers

v <- c( -1, -0.5, 0, 0.5, 1, rnorm( 10 ) )
v
 [1] -1.00000000 -0.50000000  0.00000000  0.50000000  1.00000000  0.75025362  0.06789689
 [8] -1.43082598 -0.69610590 -0.74646126 -0.65090974  0.29244999  1.73448191  1.78574121
[15] -0.91039611
round( v, 0 )
 [1] -1  0  0  0  1  1  0 -1 -1 -1 -1  0  2  2 -1
round( v, 1 )
 [1] -1.0 -0.5  0.0  0.5  1.0  0.8  0.1 -1.4 -0.7 -0.7 -0.7  0.3  1.7  1.8 -0.9
round( v, 2 )
 [1] -1.00 -0.50  0.00  0.50  1.00  0.75  0.07 -1.43 -0.70 -0.75 -0.65  0.29  1.73  1.79 -0.91
floor( v )
 [1] -1 -1  0  0  1  0  0 -2 -1 -1 -1  0  1  1 -1
ceiling( v )
 [1] -1  0  0  1  1  1  1 -1  0  0  0  1  2  2  0

Naming vector elements

heights <- c( Amy = 166, Eve = 170, Bob = 177 )
heights
Amy Eve Bob 
166 170 177 
names( heights )
[1] "Amy" "Eve" "Bob"
names( heights ) <- c( "AMY", "EVE", "BOB" )
heights
AMY EVE BOB 
166 170 177 
heights[[ "EVE" ]]
[1] 170

Generating grids

expand_grid( x = c( 1:3, NA ), y = c( "a", "b" ) )
# A tibble: 8 x 2
      x y    
  <int> <chr>
1     1 a    
2     1 b    
3     2 a    
4     2 b    
5     3 a    
6     3 b    
7    NA a    
8    NA b    

Generating combinations

combn( c( "a", "b", "c", "d", "e" ), m = 2, simplify = TRUE )
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "a"  "a"  "a"  "a"  "b"  "b"  "b"  "c"  "c"  "d"  
[2,] "b"  "c"  "d"  "e"  "c"  "d"  "e"  "d"  "e"  "e"  
combn( c( "a", "b", "c", "d", "e" ), m = 3, simplify = TRUE )
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "a"  "a"  "a"  "a"  "a"  "a"  "b"  "b"  "b"  "c"  
[2,] "b"  "b"  "b"  "c"  "c"  "d"  "c"  "c"  "d"  "d"  
[3,] "c"  "d"  "e"  "d"  "e"  "e"  "d"  "e"  "e"  "e"  


Copyright © 2021 Biomedical Data Sciences (BDS) | LUMC